using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace TmxLib
{
    public class TmxItem
    {
        private string _context;
        private TmxItemValueCollection _values = new TmxItemValueCollection();

        public TmxItem()
        {
        }

        public TmxItem(string context)
        {
            _context = context;
        }


        public string Context
        {
            get { return _context; }
        }


        public TmxItemValueCollection Values
        {
            get { return _values; }
        }
    }

    public class TmxItemCollection: Collection<TmxItem>
    {
        public void Sort()
        {
            List<TmxItem> copy = new List<TmxItem>(this);
            copy.Sort(delegate(TmxItem x, TmxItem y) { return x.Context.CompareTo(y.Context); });

            Items.Clear();
            foreach (TmxItem item in copy)
                Items.Add(item);
        }

        public string[] GetLanguages()
        {
            List<string> languages = new List<string>();
            foreach (TmxItem item in this)
            {
                foreach (TmxItemValue value in item.Values)
                    if (!languages.Contains(value.CultureName))
                        languages.Add(value.CultureName);
            }

            return languages.ToArray();
        }
    }

    public class TmxItemValue
    {
        private string _cultureName, _value, _ref;


        public TmxItemValue(string cultureName, string value, string @ref)
        {
            _cultureName = cultureName;
            _value = value;
            _ref = @ref;
        }

        public TmxItemValue(string cultureName, string value)
        {
            _cultureName = cultureName;
            _value = value;
        }


        public TmxItemValue(string cultureName)
        {
            _cultureName = cultureName;
        }


        public string CultureName
        {
            get { return _cultureName; }
            set { _cultureName = value; }
        }


        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }


        public string Ref
        {
            get { return _ref; }
            set { _ref = value; }
        }
    }

    public class TmxItemValueCollection: Collection<TmxItemValue>
    {
        public void Sort()
        {
            List<TmxItemValue> copy = new List<TmxItemValue>(this);
            copy.Sort(delegate(TmxItemValue x, TmxItemValue y) { return x.CultureName.CompareTo(y.CultureName); });

            Items.Clear();
            foreach(TmxItemValue item in copy)
                Items.Add(item);
        }

        public string this[string language]
        {
            get
            {
                foreach (TmxItemValue v in this)
                    if (v.CultureName == language)
                        return v.Value;

                return null;
            }
            set
            {
                foreach (TmxItemValue v in this)
                    if (v.CultureName == language)
                    {
                        v.Value = value;
                        return;
                    }

                Add(new TmxItemValue(language, value));
            }
        }

        public TmxItemValue Get(string language)
        {
            foreach (TmxItemValue v in this)
                if (v.CultureName == language)
                    return v;

            return null;
        }
    }
}
